package com.rightsidetech.scheduler.task;

import com.rightsidetech.commom.utils.DateUtil;
import com.rightsidetech.system.dtos.response.store.SysStoreOrderDetailResp;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.trade.entity.TdOrderIncomeSalesDayStatis;
import com.rightsidetech.trade.service.TdStatisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@EnableScheduling
public class StatisTask implements SchedulingConfigurer {

    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;
    @Resource
    private TdStatisService tdStatisService;

    public void statis() {
        log.info("定时统计");
        //System.out.println("定时打印");
        //String str = dispatchService.test001("定时打印");
        String statisTime = DateUtil.getDate(new Date(), "yyyy-MM-dd HH:mm:ss");


        List<Long> storeIds = tdStatisService.statisOrderSalesVolumeDayByStatis(statisTime);

        if (!storeIds.isEmpty()) {
            for (Long storeId : storeIds) {
                SysStoreOrderDetailResp storeDetail = sysStoreInfoService.findStoreByStoreId(storeId);
                if(StringUtils.isEmpty(storeDetail)){
                    continue;
                }
                TdOrderIncomeSalesDayStatis tdStatis = new TdOrderIncomeSalesDayStatis();
                tdStatis.setStoreInfoId(storeDetail.getStoreId());
                tdStatis.setStoreName(storeDetail.getStoreName());

                tdStatis.setAgentInfoId(storeDetail.getAgentId());
                tdStatis.setAgentName(storeDetail.getAgentName());

                tdStatis.setAreaId(storeDetail.getAreaId());
                tdStatis.setAreaName(storeDetail.getAreaName());


                tdStatisService.statisOrderSalesVolumeDay(statisTime, tdStatis);
                tdStatisService.statisIncomeRefundDay(statisTime, tdStatis);

            }
        }
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                new Runnable() {
                    @Override
                    public void run() {
                        statis();
                    }
                }, new Trigger() {
                    @Override
                    public Date nextExecutionTime(TriggerContext triggerContext) {
                        //查询任务的执行周期corn表达式.
                        String cornExpression = "0 0 23 * * ?";

                        if (null != cornExpression) {
                            CronTrigger trigger = new CronTrigger(cornExpression);
                            Date nextExec = trigger.nextExecutionTime(triggerContext);
                            return nextExec;
                        }
                        return null;
                    }
                });
    }
}
